<?php
class FriendsController extends AppController {

	public  $name = 'Friends';
	
	public $result = array( 'result' => '',
                            'message' => '',
                            'errors' => '',
                            'data' => ''
                            );
	public function beforeFilter(){
		parent::beforeFilter();
		//$this->checkAjax();
                $this->setLayout();
		$this->Auth->allow('*');
	}

	/**
	 * Render Layout
	 *
	 */
	public function beforeRender() {
        
        $this->set( 'result', $this->result );   
    }
	public function index() {
		$this->Friend->recursive = 0;
		$this->set('friends', $this->paginate());
	}
	
	
/**
 * Add friend(follow)
 * 
 *
 */	
	public function follow(){
		
		if ( $this->RequestHandler->isAjax() ) {
			
			if( !empty( $this->data ) ){
				
				$this->data['Friend']['user_id'] = $this->Bayon->userId();
				$this->data['Friend']['created'] = date('Y-m-d h:s:i', time());
				
				$this->Friend->create();
				if($this->Friend->save($this->data)){
					
					//Update user status
					$this->loadModel('UserStatus');
					//update following number
					$this->UserStatus->updateAll(
						array( 'UserStatus.value' => 'UserStatus.value + 1' ),
						array( 'UserStatus.user_id' => $this->Bayon->userId(), 'UserStatus.name_field' => BAYON_FOLLOWING )
					);
					
					//update follower number
					$this->UserStatus->updateAll(
						array( 'UserStatus.value' => 'UserStatus.value + 1' ),
						array( 'UserStatus.user_id' => $this->data['Friend']['following'], 'UserStatus.name_field' => BAYON_FOLLOWER )
					);
					
					//update user status session of following
					$this->Session->write('User.Status.following', $this->Session->read('User.Status.following') + 1);
				
					//success added
					$this->result['result'] = 1;
					$this->result['message'] = __('Followed.', true);
					$this->result['data'] = array('url' => $this->base.'/friends/unfollow', 'text' => __('Unfollow', true));
					
					//sent mail to confirm following user
					
				}else{
					
					$this->result['result'] = 0;
					$this->result['message'] = __('Can\'t follow.', true);
					
				}
			}else{
				
				$this->result['result'] = 0;
				$this->result['message'] = __('Some errors with your request, try again later.', true);
				
			}
		}else{

			//redirect to home page user try access /friend/follow
			$this->redirect('/');
			
		}
		
	}
/**
 * Unfollow friends
 * 
 */
	public function unfollow(){
		
		if ( $this->RequestHandler->isAjax() ) {
			$delete = $this->Friend->deleteAll(
				array( 
					'Friend.user_id' => $this->Bayon->userId(),
					'Friend.following' => $this->data['Friend']['following'] 
				)
			);
			if($delete){
				
				//Update user status
				$this->loadModel('UserStatus');
				//update following number
				$this->UserStatus->updateAll(
						array( 'UserStatus.value' => 'UserStatus.value - 1' ),
						array( 'UserStatus.user_id' => $this->Bayon->userId(), 'UserStatus.name_field' => BAYON_FOLLOWING )
					);
						
				//update follower number
				$this->UserStatus->updateAll(
						array( 'UserStatus.value' => 'UserStatus.value - 1' ),
						array( 'UserStatus.user_id' => $this->data['Friend']['following'], 'UserStatus.name_field' => BAYON_FOLLOWER )
					);
				
				//update user status session of following
				$this->Session->write('User.Status.following', $this->Session->read('User.Status.following') - 1);
				
				//success unfollow
				$this->result['result'] = 1;
				$this->result['message'] = __('Unfollowed.', true);
                                $this->result['data'] = array('url' => $this->base.'/friends/follow', 'text' => __('Follow', true));
				
			}else{
				
				//error unfollow
				$this->result['result'] = 0;
				$this->result['message'] = __('Can\'t unfollow.', true);
			}
		}else{
			//redirect to home page if user try access manual
			$this->redirect('/');
		}
	}
}
?>